const { app, BrowserWindow, globalShortcut, Menu, dialog, ipcMain } = require('electron');
const path = require('path');
const shutdown = require('electron-shutdown-command');
let mainWindow = null;

if (require('electron-squirrel-startup')) {
  app.quit();
}

app.commandLine.appendSwitch('unsafely-treat-insecure-origin-as-secure', ['http://zsff.gaoshiyingxiang.com/']);


// 禁用gpu加速
// app.disableHardwareAcceleration();
const onAppReady = () => {
  // 设置开机自起
  const exeName = path.basename(process.execPath);
  app.setLoginItemSettings({
    // 设置为true注册开机自起
    openAtLogin: false,
    path: process.execPath,
    args: ["--processStart", `"${exeName}"`],
  });
}
const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    fullScreen: false,
    alwaysOnTop: true,
    resizable: false,
    closable: false,
    kiosk: true,
    frame: false,
    show: false,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      contextIsolation: false,
      webviewTag: true,
      allowMediaDevices: true,
      allowRunningInsecureContent: true,
    },
  });

  mainWindow.once('ready-to-show', () => {
    mainWindow.show();
  });

  // mainWindow.loadURL("https://knowledge-server.mgtx-tech-test.mgtx.com.cn/");
  mainWindow.loadFile(path.join(__dirname, 'index.html'));
  globalShortcut.register("F11", () => {
    mainWindow.setFullScreen(true);
  })
  globalShortcut.register("ESC", () => {
    return;
  })
  mainWindow.webContents.setWindowOpenHandler(details => {
    mainWindow.loadURL(details.url)
  })

  // mainWindow.webContents.openDevTools();
  if (app.isPackaged) {
    onAppReady();
  }
  mainWindow.webContents.once('did-fail-load', function () {
    setTimeout(() => {
      mainWindow.reload();
    }, 1000);
  });

};

ipcMain.on('show-win', () => {
  console.log("show-win");
  // mainWindow.setAlwaysOnTop(true);
  mainWindow.setFullScreen(true);
})
app.on('ready', createWindow);
app.on('will-finish-launching', () => {
});
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
